import tool from "@/common/tool.js";
class HeaderPC {
  mixin = {
    data() {
      return {
        tool,
        search: {
          show: false,
          text: ""
        },
        blogList: [],
      }
    },
    computed: {
      menuList() {
        return this.$store.state.menu.menuList;
      },
      userInfo() {
        return this.$store.state.user.userInfo;
      },
      searchText() {
        return this.$store.state.over.searchText;
      },
      collectionList() {
        return this.$store.state.user.collectionList;
      },
      cartList() {
        return this.$store.state.menu.cartList;
      }
    },
    watch: {
      searchText(value) {
        this.search.text = value;
      },
      $route(to, from) {
        this.getCollectionList();
        this.getCartList();
      },
    },
    mounted() {
      this.getBlogList();
      this.getCollectionList();
      this.getCartList();
    },
    methods: {
      //获取收藏列表
      getCollectionList() {
        if (this.isLogin && this.collectionList.length == 0) {
          this.$store.dispatch("user/collectionList", {
            page: 1,
            page_size: 10000
          }); //获取收藏列表
        }
      },
      //获取购物车列表
      getCartList() {
        if (this.cartList.length == 0) {
          if (this.isLogin) {
            this.$store.dispatch("menu/cartList"); //获取收藏列表
          } else {
            let cartList=tool.getStorage("cartList");
            this.$store.commit('menu/setCartList', cartList?cartList:[]); //保存购物车列表
          }
        }
      },
      //点击搜索
      clickSearch() {
        this.search.show = false;
        this.$store.commit("over/setSearchText", this.search.text);
        this.$router.push({
          path: "/search",
          query: {
            text: this.search.text,
          }
        });
      },
      //点击用户
      clickUser() {
        if (this.isLogin) {
          this.$router.push("/user");
        } else {
          this.$router.push({
            path: "/login",
            query: {
              redirect: "/"
            }
          });
        }
      },
      //获取博客列表
      async getBlogList() {
        let res = await this.$store.dispatch("blog/blogList", {
          page: 1,
          page_size: 7,
          column_id: "",
        });
        if (res.code == 0 && res.data != undefined) {
          this.blogList = res.data.list;
        }
      },
    }
  }
}
export default new HeaderPC();
